﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Common;
using GameServer.Servers;
using GameServer.DAO;
using GameServer.Model;

namespace GameServer.Controller
{
    class UserController : BaseController
    {
        private UserDAO userDAO = new UserDAO();
        private ResultDAO resultDAO = new ResultDAO();

        public UserController()
        {
            requestCode = RequestCode.User;
        }

        /// <summary>
        /// 登录
        /// </summary>
        public string Login(string data, Client client, Server server)
        {
            string[] strs = data.Split(',');
            User user = userDAO.VerifyUser(client.MySQLConn, strs[0], strs[1]);
            if (user == null)
            {
                Console.WriteLine("登陆失败");
                return ((int)ReturnCode.Fail).ToString();
            }
            else
            {
                Console.WriteLine("登陆成功");
                Result res = resultDAO.GetResultByUserId(client.MySQLConn, user.Id);
                client.SetUserData(user, res);
                return string.Format("{0},{1},{2},{3}", (int)ReturnCode.Success, user.Username, res.Total, res.Win);
            }
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="data"></param>
        /// <param name="client"></param>
        /// <param name="server"></param>
        /// <returns></returns>
        public string Register(string data, Client client, Server server)
        {
            string[] strs = data.Split(',');
            string username = strs[0];string pwd = strs[1];
            Console.WriteLine("注册时玩家输入的昵称是：" + username + "，密码是：" + pwd);
            bool res = userDAO.CheckUser(client.MySQLConn, username);
            if (res)
                return ((int)ReturnCode.Fail).ToString();
            userDAO.AddUser(client.MySQLConn, username, pwd);
            return ((int)ReturnCode.Success).ToString();
        }
    }
}
